import { defineStore } from 'pinia'
import { ref } from 'vue';
import { login } from '@/api/login';
import { getRouters,getUserInfo } from '@/api/index';
import { getToken, removeToken, setToken } from '@/utils/auth';
export const useUserStore = defineStore('useUserStore', () => {
  const token = ref(getToken());
  const roles = ref([]);//角色
  const menus = ref([]);//菜单列表
  const permissions = ref(['admin']);//按钮权限

  /**登录*/
  const loginFun = (userInfo) => {
    return new Promise((resolve, reject) => {
      login(userInfo).then(res => {
        setToken(res.data.token);
        token.vlaue = res.data.token;
        resolve()
      });
    })
  }
  

  /**获取用户信息*/
  const getUserInfoFun = () => {
    return new Promise((resolve, reject) => {
      getUserInfo().then(res => {
        roles.value = ['admin']
        resolve(res);
      });
    })
  }

  /**获取用户路由*/
  const generateRoutes = () => {
    return new Promise((resolve, reject) => {
      getRouters().then(res => {
        resolve(res.data)
      });
    })
  }

  /**退出系统*/
  const logOut = () => {
    return new Promise((resolve, reject) => {
      removeToken();
      resolve();
    });
  }
  return {
    roles,
    loginFun,
    getUserInfoFun,
    logOut,
    generateRoutes,
    menus,
    permissions
  }
})